package cn.com.ccxe.service;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.com.ccxe.core.entity.Favorite;
import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.dao.FavoriteDao;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.impl.FavoriteDaoImpl;

public class FavoriteBussiness {

	private FavoriteDao favoriteDao = new FavoriteDaoImpl();
	
	public List<Favorite> getAllByUser( String userId) {
		if(isEmpty(userId)) {
			return favoriteDao.getAllByUser(userId);
		}
		return null;
	}

	private boolean isEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	/**
	 * 先删除站点关键字关联表中的数据，再删除收藏夹得数据
	 * @param id
	 * @return
	 */
	public OperationStatus del(int id) {
		if(id > 0) {
			return favoriteDao.del(id);
		}
		return OperationStatus.FAILURE;
	}
	
	/**
	 * 为收藏的站点设定关键字
	 * @param favoriteId
	 * @param keywordId
	 * @return
	 */
	public OperationStatus addKeyword(int favoriteId, int keywordId) {
		if(!(favoriteId > 0 && keywordId > 0)) {
			throw new NullPointerException("参数为空！");
		}
		return favoriteDao.addKeyword(favoriteId, keywordId);
	}
	
	public List<Keyword> getKeywordListIsNot(int monitorTypeId, String userId, int websiteId) {
		if(monitorTypeId >0 && isEmpty(userId)) {
			return favoriteDao.getKeywordListIsNot(monitorTypeId, userId, websiteId);
		}
		return null;
	}

	public List<Keyword> getKeywordList(int monitorTypeId, String userId,int websiteId) {
		if(monitorTypeId >0 && isEmpty(userId)) {
			return favoriteDao.getKeywordList(monitorTypeId, userId, websiteId);
		}
		return null;
	}

	public OperationStatus delKeyword(int favoriteId, int keywordId) {
		if(!(favoriteId <=0 || keywordId <=0)) {
			return favoriteDao.delKeyword(favoriteId,keywordId);
		}
		return OperationStatus.FAILURE;
	}

	public OperationStatus addFavoriteKeyword(int keywordId, int moduleId,String userId) {
		if(moduleId <=0 
				|| keywordId <=0 
				|| userId == null 
				|| ("".equals(userId))) {
			return OperationStatus.FAILURE;
		}
		return favoriteDao.addFavoriteKeyword(keywordId, moduleId, userId);
	}
	
	public OperationStatus delFavoriteKeyword(int keywordId, int moduleId,String userId) {
		if(moduleId <=0 
				|| keywordId <=0 
				|| userId == null 
				|| ("".equals(userId))) {
			return OperationStatus.FAILURE;
		}
		return favoriteDao.delFavoriteKeyword(keywordId, moduleId, userId);
	}

	public List<Keyword> getSelectedKeywordList(int moduleId, String userId) {
		if(!(moduleId <=0 || userId == null || !"".equals(userId))) {
			return Collections.emptyList();
		}
		return favoriteDao.getSelectedKeywordList(moduleId,userId);
	}

	public List<Keyword> getNotSelectedKeywordList(int moduleId, String userId) {
		if(!(moduleId <=0 || userId == null || !"".equals(userId))) {
			return Collections.emptyList();
		}
		return favoriteDao.getNotSelectedKeywordList(moduleId,userId);
	}
}
